/**
 * @description 行业类目属性管理控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const dayjs = require("dayjs")
const model = require("../../../model/product/property/PROPERTY.MODEL")
const { getFilter } = require("../../../utils/setFilter")
// 创建一个内容
const controller_save = async ({
    id = undefined,
    propertyName = undefined,
    classId = undefined,
    className = undefined,
    propertyValue = undefined,
    status = undefined, sort
}) => {
    const params = {
        id: randomUUID(), propertyName, classId, className, propertyValue: JSON.stringify(propertyValue), status, sort
    }
    let addRes = null;
    if (!id) {
        // 添加创建
        addRes = await model.create({
            ...params
        })
    } else {
        // 更新修改
        addRes = await model.findOneAndUpdate({ id: id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            id, propertyName, classId, className, propertyValue, status, sort
        } = addRes

        return {
            code: 0,
            message: "成功",
            data: {
                id, propertyName, classId, className, propertyValue, status, sort
            }
        }
    }
    return {
        status: "2000",
        message: "失败",
    }
}

// 查询列表 
const controller_find = async ({ current, pageSize, propertyName = undefined, classId = undefined, className, status = undefined }) => {
    try {
        let filter = {
            propertyName: propertyName && { $regex: eval("/" + propertyName + "/i") },//名称模糊查询
            status: status && { $in: status },
            classId: classId && { $in: classId },
            className: className && { $in: className },
        }
        filter = getFilter(filter)
        const list = await model.find(filter).sort({ sort: 1 }).skip((current - 1) * pageSize).limit(pageSize).exec();
        const totalCount = await model.countDocuments(filter);
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "查询失败",
            data: {
                list: [],
                totalCount: 0
            }
        }
    }
}


/**@description 根据id查详情 */
const controller_get = async ({ id }) => {
    const res = await model.findOne({
        id
    })
    if (res) {
        const { id, propertyName, classId, className, propertyValue, status, sort } = res;
        return {
            code: 0,
            message: "查询成功",
            data: {
                id, propertyName, classId, className, propertyValue, status, sort
            }
        }
    } else {
        return {
            status: '01',
            message: "未找到对应菜单数据"
        }
    }
}

// 删除
const controller_delete = async ({ ids = [] }) => {
    const deleteResult = await model.deleteMany({
        id: { $in: ids }
    });

    if (deleteResult.deletedCount) {
        return {
            status: '0000',
            message: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            status: '0010',
            message: `删除失败`
        }
    }

}
module.exports = {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
}